Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SRC-16 Typed Structured Data #161

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

CatspersCoffee
Copy link

@CatspersCoffee CatspersCoffee commented Dec 4, 2024

Type of change

  • New Standard (SRC16)
  • Documentation
  • Example Implementations.

Changes

The following changes have been made:

  • Added SRC-16 standard for typed structured data hashing and signing.
  • Implemented core encoding functions for atomic, dynamic and reference types.
  • Added domain separator logic for cross-protocol signature safety.
  • Created comprehensive test suite for encoders (see: tests ).
  • Added example implementation with Mail struct for both SRC16Domain (Fuel) and EIP712Domain (Ethereum).
  • Included documentation on backwards compatability.

Notes

  • This standard enhances the Fuel ecosystem by providing a consistent way to handle structured data signing.
  • Compatible with existing EVM wallet infrastructure through domain type flexibility.
  • Designed to be extensible for future data types.
  • Test coverage includes both single value, dynamic array and fixed array encoding.

Related Issues

Closes #

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
    • If my change requires substantial documentation changes, I have requested support from the DevRel team
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.
  • I have updated the changelog to reflect the changes on this PR.

@CatspersCoffee CatspersCoffee requested a review from a team as a code owner December 4, 2024 13:08
@fuel-cla-bot
Copy link

fuel-cla-bot bot commented Dec 4, 2024

Thanks for the contribution! Unfortunately we can't verify the commit author(s): Antony <b***@g***.com>. One possible solution is to add that email to your GitHub account. Alternatively you can change your commits to another email and force push the change. After getting your commits associated with your GitHub account, sign the Fuel Labs Contributor License Agreement and this Pull Request will be revalidated.

@fuel-cla-bot
Copy link

fuel-cla-bot bot commented Dec 4, 2024

Thanks for the contribution! Before we can merge this, we need @CatspersCoffee to sign the Fuel Labs Contributor License Agreement.

@SwayStar123
Copy link
Collaborator

Just giving you a heads up, we will be reviewing this standard in the following days, but Fuel is enacting a code freeze around christmas as many contributors will be on vacation etc. So this will likely not be merged until January as the earliest.

Copy link
Member

@bitzoic bitzoic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing!

Just a few questions, primarily surrounding whether the existing encoder and hashing functionality in the std-lib and core are sufficent/can be integrated or if we need to introduce new traits and structs.

I'm also curious on how handling Fuel specific types would work. We've made the distinct choice to separate the Address and ContractId types as well as have AssetId.

docs/src/src-16-typed-structured-data.md Outdated Show resolved Hide resolved
/// A demo struct representing a mail message
pub struct Mail {
/// The sender's address
pub from: b256,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub from: b256,
pub from: Address,
Suggested change
pub from: b256,
pub from: ContractId,
Suggested change
pub from: b256,
pub from: Identity,

To follow Sway principles, this should use either Address, ContractId, or Identity.
The same applies to to below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been adjusted in the Fuel example to reflect the Address native type.

/// A demo struct representing a mail message
pub struct Mail {
    /// The sender's address
    pub from: Address,
    /// The recipient's address
    pub to: Address,
    /// The message contents
    pub contents: String,
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was updated in examples/src16-typed-data/fuel_example/src/main.sw but not yet in examples/src16-typed-data/ethereum_example/src/main.sw 🙂 Happy to approve after these changes

standards/src/src16.sw Outdated Show resolved Hide resolved
standards/src/src16.sw Show resolved Hide resolved
standards/src/src16.sw Outdated Show resolved Hide resolved
examples/src16-typed-data/fuel_example/src/main.sw Outdated Show resolved Hide resolved
@bitzoic bitzoic requested a review from a team December 11, 2024 12:35
@bitzoic bitzoic added the New Standard Label used to filter for the introduction of a new standard label Dec 11, 2024
@bitzoic bitzoic added the SRC-16 Label used to filter for the standard issue label Dec 11, 2024
docs/src/src-16-typed-structured-data.md Outdated Show resolved Hide resolved
docs/src/src-16-typed-structured-data.md Show resolved Hide resolved
docs/src/src-16-typed-structured-data.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@SwayStar123 SwayStar123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest changing the pseudocode to follow sway code formatting guidelines, as this is specifically a Sway standard, but it is fine as it is aswell.

}

// EIP712 Domain (Ethereum compatible)
pub struct EIP712Domain {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not missing a salt field?

* Add SRC16_DOMAIN_TYPE_HASH
* Add Keccak256 hash of name string
* Add Keccak256 hash of version string
* Add chain ID as 32-byte big-endian
Copy link
Collaborator

@SwayStar123 SwayStar123 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the field in the struct is a u64, can you make it clear here that the bytes are to be padded?

@K1-R1
Copy link
Member

K1-R1 commented Jan 9, 2025

Looking good to me; happy to approve once above comments are addressed and CI is passing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:signed New Standard Label used to filter for the introduction of a new standard SRC-16 Label used to filter for the standard issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants